<%@ page import="com.idriver.bean.Admin" %>
<%@ page import="com.idriver.service.StudentService" %>
<%@ page import="com.idriver.bean.Student" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: wulimin
  Date: 2021/12/13
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>学生信息管理系统</title>
</head>
<body>

<%
    // 通过session判断管理员是否登录; 如果未登录, 直接跳转到登录页面
    Object admin = session.getAttribute("admin");
    System.out.println(admin);
    if (null == admin) {
        response.sendRedirect("login.jsp");
        return;
    }

    // 通过request请求对象, 获取page, size, 搜索关键字keyword这三个参数, 用来做分页数据展示
    String pageNumString = request.getParameter("page");
    String sizeNumString = request.getParameter("size");
    String keyword = request.getParameter("keyword");
    if (null == keyword) {
        keyword = "";
    }

    if (null == pageNumString) {
        pageNumString = "1";
    }
    if (null == sizeNumString) {
        sizeNumString = "2";
    }

    StudentService service = new StudentService();
    //获取总记录条数
    int totalRecordNum = service.getStudentsListNum(keyword);
    System.out.println("--------------start");
    System.out.println("总条数 = " + totalRecordNum);

    int pageNum = Integer.parseInt(pageNumString);
    int sizeNum = Integer.parseInt(sizeNumString);

    // 获取上一页下一页的页码
    int prev = 0;
    int next = 0;
    // 总页码
    int totalPageNum = (int)(Math.ceil(totalRecordNum * 1.0 / sizeNum));
    System.out.println("当前页 = " + pageNum + ", 每页条数 = " + sizeNum + ", 总页数:" + totalPageNum);

    //对上一页和下一页的限制; 上一页最小不能小于1; 下一页最大不能比总页数还多
    if (pageNum <= 1) {
        prev = 1;
    } else {
        prev = pageNum - 1;
    }

    if (pageNum >= totalPageNum) {
        next = totalPageNum;
    } else {
        next = pageNum + 1;
    }

    System.out.println("上一页:" + prev + ", 下一页:" + next);

    List<Student> studentLists = service.getStudentsLists(pageNum, sizeNum, keyword);
%>

<h2 align="center">学生信息管理系统</h2>
<div style="margin-right: 150px" align="right">
    <span>管理员&nbsp;<%= ((Admin)session.getAttribute("admin")).getName() %></span>
    &nbsp;&nbsp;
    <span><a href="logout">退出系统</a></span>
</div>
<div style="margin-top: 20px">
<div align="right" style="margin-right: 150px"><a href="addStudent.jsp" font="20px">新增学员</a></div>
</div>
<div style="margin-left: 140px">
<form action="main.jsp?page=<%= pageNum %>&size=<%= sizeNum %>&keyword=<%= keyword %>" method="get">
    <input type="text" name="keyword" value="<%= keyword %>" />
    <input  type="submit" value="搜索" />
</form>
</div>
<table style="width: 80%;" cellspacing="0px" border="1px" align="center">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>出生日期</td>
        <td>邮箱</td>
        <td>备注</td>
        <td>操作</td>
    </tr>
    <%
        for (Student student : studentLists) {
    %>
        <tr>
            <td align="center"><%= student.getId() %></td>
            <td align="center"><%= student.getName() %></td>
            <td align="center"><%= student.getSex() %></td>
            <td align="center"><%= student.getBirthday() %></td>
            <td align="center"><%= student.getMail() %></td>
            <td align="center"><%= student.getMark() %></td>
            <td align="center">
                <a href="edit.jsp?id=<%= student.getId() %>">编辑</a>
                &nbsp;|&nbsp;
                <a href="delStudent?id=<%= student.getId() %>">删除</a>
            </td>
        </tr>
    <%
        }
    %>
</table>
<div style="float: right;margin-right: 150px;margin-top:15px">
<a href="main.jsp?page=<%= prev %>&size=<%= sizeNum %>&keyword=<%= keyword%>">上一页</a>&nbsp;
<%
    for (int i = 1; i <= totalPageNum;i++) {
        String pageLink = "";
        if (i == pageNum) {
            pageLink = "<a href='main.jsp?page=" + i +"&size=" + sizeNum+ "&keyword="+ keyword +"'><span style='color:red'>" + i + "</span></a>";
        } else {
            pageLink = "<a href='main.jsp?page=" + i +"&size=" + sizeNum+ "&keyword="+keyword+"'>" + i + "</a>";
        }

        System.out.println("i = " + i);
        System.out.println(pageLink);
        out.print("&nbsp;" + pageLink + "&nbsp;");
    }
%>
<a href="main.jsp?page=<%= next %>&size=<%= sizeNum %>&keyword=<%= keyword%>">下一页</a>
</div>
</body>
</html>
